package jmine.tec.web.wicket.pages.security;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import javax.security.auth.Subject;

import jmine.tec.web.wicket.pages.HelpTextMessages;
import jmine.tec.web.wicket.pages.Template;
import jmine.tec.web.wicket.security.SecureSession;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;

import bancosys.tec.exception.MessageCreator;
import bancosys.tec.security.annotation.Secure;
import bancosys.tec.security.authorization.Permission;
import bancosys.tec.security.authorization.UrlPermission;

/**
 * Tela de visualização de permissões para o usuário logado.
 * 
 * @author diego.ferreira
 * @created Jan 7, 2011
 */
@Secure(permissionType = UrlPermission.class)
public class UsuarioPermissoes extends Template {

    private List<Permission> permissoes = new ArrayList<Permission>();

    /**
     * Construtor
     */
    public UsuarioPermissoes() {
        this.generateComponents();
    }

    /**
     * Gera os componentes utilizados na tela.
     */
    private void generateComponents() {
        Set<Permission> publicCredentials;
        Set<Permission> privateCredentials;
        String username;

        Subject subject = SecureSession.getSecureSession().getSubject();
        if (subject != null) {
            Principal principal = subject.getPrincipals().iterator().next();
            publicCredentials = subject.getPublicCredentials(Permission.class);
            privateCredentials = subject.getPrivateCredentials(Permission.class);
            username = principal.getName();
        } else {
            publicCredentials = Collections.emptySet();
            privateCredentials = Collections.emptySet();
            username = "";
        }

        this.add(new Label("username", username));
        this.permissoes.addAll(publicCredentials);
        this.permissoes.addAll(privateCredentials);

        Collections.sort(this.permissoes, new Comparator<Permission>() {
            public int compare(Permission o1, Permission o2) {
                return new CompareToBuilder().append(o1.getType().getSimpleName(), o2.getType().getSimpleName())
                        .append(o1.getPermissionId(), o2.getPermissionId()).toComparison();
            }
        });

        this.add(new ListView<Permission>("publicCredentials", this.permissoes) {

            @Override
            protected void populateItem(ListItem<Permission> item) {
                Permission permission = item.getModelObject();
                item.add(new Label("type", permission.getType().getSimpleName()));
                item.add(new Label("id", permission.getPermissionId()));
            }
        });
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected MessageCreator getHelpTextCreator() {
        return HelpTextMessages.USUARIO_PERMISSOES;
    }
}
